<?php defined('BASEPATH') or die("No access");
    class Cart_model extends MY_Model
    {
        public function __construct()
        {
            parent::__construct();
        }
        //get all cart
        public function getAll($limit,$offset)
        {
            $this->db->order_by('cart.cart_day','desc');
            $this->db->limit($limit,$offset);
            $this->db->from('cart');
            $this->db->join('customer','customer.customer_id=cart.customer_cart');
            $this->db->join('status_cart','status_cart.status_cart_id=cart.cart_status');
            $query=$this->db->get();
            return $query->result(); 
        }
        //get cart by id 
        public function getCartById($id)
        {
            $this->db->where('cart_detail.cart_detail_cart',$id);
            $this->db->from('cart_detail');
            $this->db->join('cart','cart.cart_id=cart_detail_cart');
            $this->db->join('product','product.product_id=cart_detail.cart_product');
            $this->db->join('customer','customer.customer_id=cart.customer_cart');
            $this->db->join('status_cart','status_cart.status_cart_id=cart.cart_status');
            $query=$this->db->get();
            return $query->result();
        }
        
        //get shipping
        public function getShipping($city,$district)
        {
            $this->db->where('district_id',$district);
            $this->db->where('district_city',$city);
            $query=$this->db->get('district');
            $row=$query->row();
            $area=$row->district_area;
            $this->db->where('area_id',$area);
            $query2=$this->db->get('area');
            $result=$query2->row();
           
            $shipping=$result->area_shipping;
            return $shipping;   
        }
        
        // insert or update 
        public function save($id,$data=array())
        {
            if($id==0)
            {
                //insert new record
                $this->db->insert('cart',$data);
            }
            else
            {
                $this->db->where('cart_id',$id);
                $this->db->update('cart',$data);
            }
            return $this->db->affected_rows();
        }
        
        public function saveDetailCart($id,$data=array())
        {
            if($id==0)
            {
                //insert new record
                $this->db->insert('cart_detail',$data);
            }
            else
            {
                $this->db->where('cart_detail_id',$id);
                $this->db->update('cart_detail',$data);
            }
            return $this->db->affected_rows();
        }
        //delete array carts
        public function delete($id=array())
        {
            $this->db->where_in('cart_id',$id);
            $this->db->delete('cart');
            return $this->db->affected_rows();
        }
        //publish or unpublish
        public function publish($status,$id=array())
        {
            $this->db->where_in('cart_id',$id);
            if($status==0)
            {
                $this->db->update('cart',array('cart_publish'=>0));
            }
            else
            {
                $this->db->update('cart',array('cart_publish'=>1));
            }
            return $this->db->affected_rows();
        }
    }
?>

